package com.meida.module.marketing.provider.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.mybatis.interceptor.PageInterceptor;
import com.meida.common.mybatis.model.PageParams;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.DateUtils;
import com.meida.module.marketing.client.entity.MkgCoupon;
import com.meida.module.marketing.client.entity.MkgCouponUser;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 用户优惠券列表
 *
 * @author zyf
 */
@Component("userCouponListHandler")
public class UserCouponListHandler implements PageInterceptor<MkgCouponUser> {


    @Override
    public ResultBody validate(CriteriaQuery cq, EntityMap params) {
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaQuery cq, PageParams pageParams, EntityMap params) {
        Integer useState = params.getInt("useState", 0);
        Long userId = OpenHelper.getUserId();
        cq.select(MkgCoupon.class, "*");

        cq.eq(MkgCouponUser.class, "userId", userId);
        //未使用
        if (useState.equals(0)) {
            cq.ge("userExpireEndDate", DateUtils.getDateTime());
        }
        //已使用
        if (useState.equals(1)) {
            cq.eq(MkgCouponUser.class, "useState", 1);
        }
        //已过期
        if (useState.equals(2)) {
            cq.lt("userExpireEndDate", DateUtils.getDateTime());
            cq.eq(MkgCouponUser.class, "useState", 0);
        }
    }

    @Override
    public void complete(CriteriaQuery<MkgCouponUser> cq, List<EntityMap> result, EntityMap extra) {

    }

}
